package com.zy.openchat.util;

import androidx.annotation.NonNull;

import com.ja.openchat.R;
import com.zy.openchat.App;

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;

import java.util.Calendar;
import java.util.Date;

/**
 * @创建者 CSDN_LQR
 * @描述 时间工具（需要joda-time）
 */
public class TimeUtils {

    public static String getMsgFormatHMTime(long msgTimeMillis){
        if (msgTimeMillis == 0) {
            return "";
        }
        DateTime msgTime = new DateTime(msgTimeMillis);
        return getTime(msgTime);
    }

    public static String getMsgFormatMSTime(long msgTimeMillis){
        if (msgTimeMillis == 0) {
            return "";
        }
        DateTime msgTime = new DateTime(msgTimeMillis);
        return msgTime.toString("mm分ss秒");
    }

    /**
     * 得到仿微信日期格式输出
     *
     * @param msgTimeMillis
     * @return
     */
    public static String getMsgFormatTime(long msgTimeMillis) {
        if (msgTimeMillis == 0) {
            return "";
        }

        DateTime msgTime = new DateTime(msgTimeMillis);
        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.get(Calendar.DAY_OF_YEAR);
        Calendar msgCalendar = Calendar.getInstance();
        msgCalendar.setTimeInMillis(msgTimeMillis);

        if (nowCalendar.get(Calendar.YEAR) == msgCalendar.get(Calendar.YEAR)) {
            if (nowCalendar.get(Calendar.DAY_OF_YEAR) == msgCalendar.get(Calendar.DAY_OF_YEAR)) {
                //早上、下午、晚上 1:40
                return getTime(msgTime);
            } else if (msgCalendar.get(Calendar.DAY_OF_YEAR) + 1 == nowCalendar.get(Calendar.DAY_OF_YEAR)) {
                //昨天
                return App.getInstanse().getString(R.string.yesterday_text)+" " + getTime(msgTime);
            } else if (msgCalendar.get(Calendar.DAY_OF_YEAR) + 2 == nowCalendar.get(Calendar.DAY_OF_YEAR)) {
                return App.getInstanse().getString(R.string.the_day_before_yesterday)+" " + getTime(msgTime);
            }  else if (nowCalendar.get(Calendar.WEEK_OF_YEAR) == msgCalendar.get(Calendar.WEEK_OF_YEAR)) {
                //星期
                switch (msgTime.getDayOfWeek()) {
                    case DateTimeConstants.SUNDAY:
                        return App.getInstanse().getString(R.string.sunday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.MONDAY:
                        return App.getInstanse().getString(R.string.monday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.TUESDAY:
                        return App.getInstanse().getString(R.string.tuesday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.WEDNESDAY:
                        return App.getInstanse().getString(R.string.wednesday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.THURSDAY:
                        return App.getInstanse().getString(R.string.thursday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.FRIDAY:
                        return App.getInstanse().getString(R.string.friday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.SATURDAY:
                        return App.getInstanse().getString(R.string.saturday_text)+" " + getTime(msgTime);
                    default:
                        break;
                }
                return "";
            } else {
                //12月22日
                return msgTime.toString("MM月dd日 HH:mm");
            }
        } else {
            return msgTime.toString("yyyy年MM月dd日 HH:mm");
        }
    }


    public static String getMsgFormatTimeForYYMMDD(long msgTimeMillis) {
        if (msgTimeMillis == 0) {
            return "";
        }

        DateTime msgTime = new DateTime(msgTimeMillis);
        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.get(Calendar.DAY_OF_YEAR);
        Calendar msgCalendar = Calendar.getInstance();
        msgCalendar.setTimeInMillis(msgTimeMillis);

        if (nowCalendar.get(Calendar.YEAR) == msgCalendar.get(Calendar.YEAR)) {
            if (nowCalendar.get(Calendar.DAY_OF_YEAR) == msgCalendar.get(Calendar.DAY_OF_YEAR)) {
                //早上、下午、晚上 1:40
                return App.getInstanse().getString(R.string.today_text);
            } else if (msgCalendar.get(Calendar.DAY_OF_YEAR) + 1 == nowCalendar.get(Calendar.DAY_OF_YEAR)) {
                //昨天
                return App.getInstanse().getString(R.string.yesterday_text);
            } else if (msgCalendar.get(Calendar.DAY_OF_YEAR) + 2 == nowCalendar.get(Calendar.DAY_OF_YEAR)) {
                //昨天
                return App.getInstanse().getString(R.string.the_day_before_yesterday);
            } else if (nowCalendar.get(Calendar.WEEK_OF_YEAR) == msgCalendar.get(Calendar.WEEK_OF_YEAR)) {
                //星期
                switch (msgTime.getDayOfWeek()) {
                    case DateTimeConstants.SUNDAY:
                        return App.getInstanse().getString(R.string.sunday_text);
                    case DateTimeConstants.MONDAY:
                        return App.getInstanse().getString(R.string.monday_text);
                    case DateTimeConstants.TUESDAY:
                        return App.getInstanse().getString(R.string.tuesday_text);
                    case DateTimeConstants.WEDNESDAY:
                        return App.getInstanse().getString(R.string.wednesday_text);
                    case DateTimeConstants.THURSDAY:
                        return App.getInstanse().getString(R.string.thursday_text);
                    case DateTimeConstants.FRIDAY:
                        return App.getInstanse().getString(R.string.friday_text);
                    case DateTimeConstants.SATURDAY:
                        return App.getInstanse().getString(R.string.saturday_text);
                    default:
                        break;
                }
                return "";
            } else {
                //12月22日
                return msgTime.toString("yyyy-MM-dd");
            }
        } else {
            return msgTime.toString("yyyy-MM-dd");
        }
    }


    public static String getMsgFormatTimeYYMMDDHHMM(long msgTimeMillis) {
        if (msgTimeMillis == 0) {
            return "";
        }

        DateTime msgTime = new DateTime(msgTimeMillis);
        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.get(Calendar.DAY_OF_YEAR);
        Calendar msgCalendar = Calendar.getInstance();
        msgCalendar.setTimeInMillis(msgTimeMillis);

        if (nowCalendar.get(Calendar.YEAR) == msgCalendar.get(Calendar.YEAR)) {
            if (nowCalendar.get(Calendar.DAY_OF_YEAR) == msgCalendar.get(Calendar.DAY_OF_YEAR)) {
                //早上、下午、晚上 1:40
                return getTime(msgTime);
            } else if (msgCalendar.get(Calendar.DAY_OF_YEAR) + 1 == nowCalendar.get(Calendar.DAY_OF_YEAR)) {
                //昨天
                return App.getInstanse().getString(R.string.yesterday_text)+" " + getTime(msgTime);
            } else if (msgCalendar.get(Calendar.DAY_OF_YEAR) + 2 == nowCalendar.get(Calendar.DAY_OF_YEAR)) {
                return App.getInstanse().getString(R.string.the_day_before_yesterday)+" " + getTime(msgTime);
            }  else if (nowCalendar.get(Calendar.WEEK_OF_YEAR) == msgCalendar.get(Calendar.WEEK_OF_YEAR)) {
                //星期
                switch (msgTime.getDayOfWeek()) {
                    case DateTimeConstants.SUNDAY:
                        return App.getInstanse().getString(R.string.sunday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.MONDAY:
                        return App.getInstanse().getString(R.string.monday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.TUESDAY:
                        return App.getInstanse().getString(R.string.tuesday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.WEDNESDAY:
                        return App.getInstanse().getString(R.string.wednesday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.THURSDAY:
                        return App.getInstanse().getString(R.string.thursday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.FRIDAY:
                        return App.getInstanse().getString(R.string.friday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.SATURDAY:
                        return App.getInstanse().getString(R.string.saturday_text)+" " + getTime(msgTime);
                    default:
                        break;
                }
                return "";
            } else {
                //12月22日
                return msgTime.toString("yyyy-MM-dd HH:mm");
            }
        } else {
            return msgTime.toString("yyyy-MM-dd HH:mm");
        }
    }

    @NonNull
    private static String getTime(DateTime msgTime) {
        return msgTime.toString("HH:mm");
    }

    public static String getMsgFormatYMDTime(long msgTimeMillis){
        if (msgTimeMillis == 0) {
            return "";
        }
        DateTime msgTime = new DateTime(msgTimeMillis);
        return msgTime.toString("yyyy-MM-dd");
    }


    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    public final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年

    public static String onlineText(long msgTimeMillis){
        Date d = new Date(msgTimeMillis);
        long diff = new Date().getTime() - d.getTime();
        long r = 0;
        if (diff > year) {
            r = (diff / year);
            return r +" "+App.getInstanse().getString(R.string.years_ago);
        } else if (diff > month) {
            r = (diff / month);
            return r +" "+App.getInstanse().getString(R.string.month_ago);
        } else if (diff > day) {
            r = (diff / day);
            return r +" "+ App.getInstanse().getString(R.string.days_ago);
        } else if (diff > hour) {
            r = (diff / hour);
            return r +" "+ App.getInstanse().getString(R.string.hours_ago);
        } else if (diff > minute) {
            r = (diff / minute);
           return r +" "+ App.getInstanse().getString(R.string.minutes_ago);
        } else{
            return App.getInstanse().getString(R.string.just_now);
        }
    }


    public static String getTransferFormatTime(long msgTimeMillis) {
        if (msgTimeMillis == 0) {
            return "";
        }

        DateTime msgTime = new DateTime(msgTimeMillis);
        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.get(Calendar.DAY_OF_YEAR);
        Calendar msgCalendar = Calendar.getInstance();
        msgCalendar.setTimeInMillis(msgTimeMillis);

        if (nowCalendar.get(Calendar.YEAR) == msgCalendar.get(Calendar.YEAR)) {
            if (nowCalendar.get(Calendar.DAY_OF_YEAR) == msgCalendar.get(Calendar.DAY_OF_YEAR)) {
                //早上、下午、晚上 1:40
                return App.getInstanse().getString(R.string.today_text)+" " + getTime(msgTime);
            } else if (msgCalendar.get(Calendar.DAY_OF_YEAR) + 1 == nowCalendar.get(Calendar.DAY_OF_YEAR)) {
                //昨天
                return App.getInstanse().getString(R.string.yesterday_text)+" " + getTime(msgTime);
            } else if (msgCalendar.get(Calendar.DAY_OF_YEAR) + 2 == nowCalendar.get(Calendar.DAY_OF_YEAR)) {
                return App.getInstanse().getString(R.string.the_day_before_yesterday)+" " + getTime(msgTime);
            }  else if (nowCalendar.get(Calendar.WEEK_OF_YEAR) == msgCalendar.get(Calendar.WEEK_OF_YEAR)) {
                //星期
                switch (msgTime.getDayOfWeek()) {
                    case DateTimeConstants.SUNDAY:
                        return App.getInstanse().getString(R.string.sunday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.MONDAY:
                        return App.getInstanse().getString(R.string.monday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.TUESDAY:
                        return App.getInstanse().getString(R.string.tuesday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.WEDNESDAY:
                        return App.getInstanse().getString(R.string.wednesday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.THURSDAY:
                        return App.getInstanse().getString(R.string.thursday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.FRIDAY:
                        return App.getInstanse().getString(R.string.friday_text)+" " + getTime(msgTime);
                    case DateTimeConstants.SATURDAY:
                        return App.getInstanse().getString(R.string.saturday_text)+" " + getTime(msgTime);
                    default:
                        break;
                }
                return "";
            } else {
                //12月22日
                return msgTime.toString("MM月dd日 HH:mm");
            }
        } else {
            return msgTime.toString("yyyy年MM月dd日 HH:mm");
        }
    }

}
